import Vue from 'vue'
import Vuex from 'vuex'
import user from "./user"
import storage from '@/storage'
import keys from '@/storage/keys'

Vue.use(Vuex)
const storageState = window.localStorage
export default new Vuex.Store({
  modules: {
    user
  },
  state: {
    banner: [],
    menu: [
      {
        title: 'banner管理',
        type: 'banner',
        icon: 'icon_banner',
      },
      {
        title: '客户管理',
        type: 'customer',
        icon: 'el-icon-s-custom',
      },
      {
        title: '客户账单管理',
        type: 'bill',
        icon: 'el-icon-s-custom',
      },
      {
        title: '服务管理',
        type: 'content',
        icon: 'el-icon-folder-opened',
        list: [
          {
            title: '工商服务',
            type: 'content1',
            id: 1,
          },
          {
            title: '代理记账',
            type: 'content2',
            id: 2,
          },
          {
            title: '财税审计',
            type: 'content3',
            id: 3,
          },
          {
            title: '金融咨询',
            type: 'content4',
            id: 4,
          },
          {
            title: '知识产权',
            type: 'content5',
            id: 5,
          },
          {
            title: '科技服务',
            type: 'content6',
            id: 6,
          },
          {
            title: '资质办理',
            type: 'content7',
            id: 7,
          },
          {
            title: '法律服务',
            type: 'content8',
            id: 8,
          },
          {
            title: '外贸进出口服务',
            type: 'content9',
            id: 9,
          },
        ]
      },
      {
        title: '商学院',
        type: 'business',
        icon: 'icon_xueyuan',
      },
      // {
      //   title: '问题管理',
      //   type: 'problem',
      //   icon: 'el-icon-copy-document',
      // },
      {
        title: '新闻中心',
        type: 'news',
        icon: 'icon_news',
      },
      {
        title: '联系我们',
        type: 'contact',
        icon: 'icon_contact'
      },
      {
        title: '合作伙伴管理',
        type: 'partner',
        icon: 'icon_banner',
      }
    ],
    customerInfo: {},
    metaTitle: '首页',
    editInfo: {},
  },
  getters: {
    banner: state => {
      let loadFromStorage = async () => {
        storage.getItem(keys.banner).then(data => {
          data && (state.banner = data)
        })
      }
      if (state.banner.length === 0) {
        loadFromStorage()
      }
      console.log("1111111111")
      return state.banner
    },
    customerInfo: state => {
      let loadFromStorage = async () => {
        storage.getItem(keys.customerInfo).then(data => {
          data && (state.customerInfo = data)
        })
      }
      console.log("2222222222")
      if (Object.keys(state.customerInfo).length === 0) {
        loadFromStorage()
      }
      return state.customerInfo
    },
    metaTitle: state => {
      return state.metaTitle
    },
    editInfo: state => {
      state.editInfo = JSON.parse(storageState.getItem('editInfo'))
      return state.editInfo
    },
  },
  mutations: {
    // 更新 banner图片
    async updateBannerData(state, data) {
      storage.setItem(keys.banner, data)
    },
    // 更新 customerInfo 客户资料详情
    async updateCustomerInfoData(state, data) {
      storage.setItem(keys.customerInfo, data)
    },
    // 更新 customerInfo 客户资料详情
    async updateMetaTitleData(state, data) {
      state.metaTitle = data
    },
    // 更新 editInfo
    async updateEditInfoData(state, data) {
      state.editInfo = data
      storageState.setItem('editInfo', data)
    },
  }
})